//
//  WCProjectOutlineView.m
//  WabbitCode
//
//  Created by William Towe on 1/23/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCProjectOutlineView.h"
#import "WCMacros.h"
#import "WCActionObject.h"
#import "WCProject.h"
#import "NSTreeController+WCExtensions.h"
#import "WCInterfacePerformer.h"


NSString *kWCProjectOutlineViewPboardType = @"kWCProjectOutlineViewPboardType";

@implementation WCProjectOutlineView
- (void)awakeFromNib {
	[self registerForDraggedTypes:[NSArray arrayWithObjects:kWCProjectOutlineViewPboardType, NSFilenamesPboardType, nil]];
	[self setDraggingSourceOperationMask:(NSDragOperationMove | NSDragOperationCopy) forLocal:YES];
	[self setDraggingSourceOperationMask:(NSDragOperationCopy) forLocal:NO];
}

- (NSMenu *)menuForEvent:(NSEvent *)event {
	if (([event type] == NSLeftMouseDown && [event modifierFlags] & NSControlKeyMask) ||
		([event type] == NSRightMouseDown)) {
		
		NSInteger row = [self rowAtPoint:[self convertPointFromBase:[event locationInWindow]]];
		
		if (row != -1) {
			if (![[self selectedRowIndexes] containsIndex:row])
				[self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
		}
		[[self window] makeFirstResponder:self];
		return [self menu];
	}
	return nil;
}

- (void)keyDown:(NSEvent *)event {
	switch ([event keyCode]) {
		case kKeyCodeDelete:
		case kKeyCodeForwardDelete:
			if (![[[[self window] windowController] document] canDeleteObjects:[[self dataSource] selectedRepresentedObjects]]) {
				NSBeep();
				return;
			}
			[[[[self window] windowController] document] delete:nil];
			return;
		case kKeyCodeEnter:
		case kKeyCodeReturn:
			[self sendAction:[self doubleAction] to:[self target]];
			return;
		case kKeyCodeTab:
			if ([event modifierFlags] & NSShiftKeyMask)
				[[[[self window] windowController] document] cycleToPreviousFirstResponder];
			else
				[[[[self window] windowController] document] cycleToNextFirstResponder];
			return;
		default:
			break;
	}
	[super keyDown:event];
}
@end
